<template>
  <div>
    <div class="header">
      <span style="margin-left: 10px; text-align: right">关键字</span>
      <el-input placeholder="请输入关键字" v-model="req.keyword" style="width: 200px; margin-left: 10px"></el-input>

      <el-button plain type="primary" style="margin-left: 34px" icon="el-icon-search" @click="getInvoiceTypes()">
        搜索
      </el-button>
      <el-button type="success" style="margin-left: 10px" icon="el-icon-plus" @click="
        addVis = true
        ">
        新增
      </el-button>
    </div>
    <div class="main">
      <el-table style="overflow: auto" height="100%" :data="res.Items" stripe border highlight-current-row>
        <el-table-column type="index" width="40"></el-table-column>
        <el-table-column label="名称" width="200">
          <template slot-scope="scope">
            {{ scope.row.INTY_Name }}
          </template>
        </el-table-column>
        <el-table-column label="排序" width="100">
          <template slot-scope="scope">
            <span>{{ scope.row.INTY_Index }}</span>
          </template>
        </el-table-column>
        <el-table-column label="性质" width="200">
          <template slot-scope="scope">
            <span v-if="scope.row.INTY_ExpCaseCost">不统计到案件成本中</span>
          </template>
        </el-table-column>
        <el-table-column label="备注">
          <template slot-scope="scope">
            {{ scope.row.INTY_Remark }}
          </template>
        </el-table-column>
        <el-table-column fixed="right" label="操作" width="160">
          <template slot-scope="scope">
            <el-button type="text" size="small" icon="el-icon-delete"
              @click="deleteInvoiceType(scope.row.INTY_ID)"></el-button>
            <el-button type="text" size="small" icon="el-icon-edit" @click="curType = scope.row
            editVis = true"></el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <el-pagination layout="total,prev,pager,next,sizes" style="float: right" background
      :current-page.sync="req.pageIndex" :page-size="req.pageSize" :total="res.Total" :page-sizes="[12, 18]"
      @size-change="
        (val) => {
          req.pageSize = val
          getInvoiceTypes()
        }
      " @current-change="getInvoiceTypes()"></el-pagination>

    <el-drawer title="新增" :visible.sync="addVis" :with-header="false" direction="rtl" size="520px">
      <el-form v-if="newType" label-width="60px" label-position="left" style="
          margin-left: 20px;
          margin-top: 50px;
          overflow: auto;
          height: 100vh;
        ">
        <el-form-item label="名称">
          <el-input v-model="newType.INTY_Name" style="width: 350px"></el-input>
        </el-form-item>
        <el-form-item label="备注">
          <el-input v-model="newType.INTY_Remark" style="width: 350px" type="textarea" rows="4"></el-input>
        </el-form-item>
        <el-form-item label="排序">
          <el-input-number v-model="newType.INTY_Index" @change="handleChange" :min="1" :max="10"
            label="描述文字"></el-input-number>
        </el-form-item>
        <el-form-item>
          <el-checkbox v-model="newType.INTY_ExpCaseCost">非案件成本</el-checkbox>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="addInvoiceType()">录入</el-button>
          <el-button type="text" @click="addVis = false">取消</el-button>
        </el-form-item>
      </el-form>
    </el-drawer>

    <el-drawer title="修改" :visible.sync="editVis" :with-header="false" direction="rtl" size="520px">
      <el-form v-if="curType" label-width="60px" label-position="left" style="
          margin-left: 20px;
          margin-top: 50px;
          overflow: auto;
          height: 100vh;
        ">
        <el-form-item label="名称">
          <el-input v-model="curType.INTY_Name" style="width: 350px" disabled></el-input>
        </el-form-item>
        <el-form-item label="备注">
          <el-input v-model="curType.INTY_Remark" style="width: 350px" type="textarea" rows="4"></el-input>
        </el-form-item>
        <el-form-item label="排序">
          <el-input-number v-model="curType.INTY_Index" @change="handleChange" :min="1" :max="10"
            label="描述文字"></el-input-number>
        </el-form-item>
        <el-form-item>
          <el-checkbox v-model="curType.INTY_ExpCaseCost">非案件成本</el-checkbox>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="editInvoiceType()">保存</el-button>
          <el-button type="text" @click="editVis = false">取消</el-button>
        </el-form-item>
      </el-form>
    </el-drawer>
  </div>
</template>
<script>
import {
  getInvoiceTypes,
  addInvoiceType,
  deleteInvoiceType,
  editInvoiceType
} from '@/api/core/invoiceType'
export default {
  data: function () {
    return {
      req: {
        keyword: null,
        pageIndex: 1,
        pageSize: 12,
      },
      res: {
        Items: [],
        Total: 0,
      },
      newType: {},
      curType: null,

      addVis: false,
      editVis: false,
    }
  },
  methods: {
    addInvoiceType() {
      addInvoiceType(this.newType).then(() => {
        this.getInvoiceTypes()
        this.addVis = false
      })
    },
    editInvoiceType() {
      editInvoiceType(this.curType).then(() => {
        this.getInvoiceTypes()
        this.editVis = false
      })
    },
    getInvoiceTypes: function () {
      getInvoiceTypes(this.req).then(({ data }) => {
        this.res = data
      })
    },
    deleteInvoiceType(id) {
      this.$confirm(`是否删除`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }).then(() => {
        deleteInvoiceType(id).then(() => {
          this.getInvoiceTypes()
        })
      })
    },
  },
  created: function () {
    var data = this.$store.getters['pagination/getRoutePage'](this.$route)
    Object.assign(this.$data, data)
    this.getInvoiceTypes()
  },
  beforeRouteLeave(to, from, next) {
    this.$store.dispatch('pagination/setRoutePage', {
      route: this.$route,
      pageData: this.$data,
    })
    next()
  },
}
</script>
<style scoped>
.header {
  width: 100%;
  padding-top: 10px;
  padding-bottom: 10px;
  height: auto;
  font-size: 14px;
  font-family: PingFangSC-Regular, PingFang SC;
  font-weight: 400;
  background: #ffffff;
  border-radius: 5px;
  border-bottom: #f4f8ff 2px solid;
  display: flex;
  margin-bottom: 11px;
  align-items: center;
}

.main {
  height: calc(100% - 160px);
}

.dialogLabel {
  margin-left: 10px;
  width: 100px;
  text-align: right;
  display: inline-block;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 300px;
  height: 220px;
  line-height: 220px;
  text-align: center;
  border-style: dashed;
  border-width: thin;
}

.avatar {
  width: 300px;
  height: 220px;
  display: block;
  border-style: dashed;
  border-width: thin;
}
</style>

<style>
.el-table .warning-row {
  background-color: oldlace;
}
</style>
